package com.app.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.app.application.QXApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：lblbh on 2017/6/30 14:06
 * 邮箱：lanbuhan@163.com
 * 功能：TODO
 */
public class OpenLocalMapUtil{

        /**
         * 地图应用是否安装
         *
         * @return
         */
        public static boolean isGdMapInstalled() {
            return isAvilible(QXApplication.getContext(),"com.autonavi.minimap");
//            return isInstallPackage("com.autonavi.minimap");

        }

        public static boolean isBaiduMapInstalled() {
            return isAvilible(QXApplication.getContext(),"com.baidu.BaiduMap");
//            return isInstallPackage("com.baidu.BaiduMap");
        }


        /**
         * 检查手机上是否安装了指定的软件
         * @param context
         * @param packageName：应用包名
         * @return
         */
        private static boolean isAvilible(Context context, String packageName){
            //获取packagemanager
            final PackageManager packageManager = context.getPackageManager();
            //获取所有已安装程序的包信息
            List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
            //用于存储所有已安装程序的包名
            List<String> packageNames = new ArrayList<String>();
            //从pinfo中将包名字逐一取出，压入pName list中
            if(packageInfos != null){
                for(int i = 0; i < packageInfos.size(); i++){
                    String packName = packageInfos.get(i).packageName;
                    packageNames.add(packName);
                }
            }
            //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
            return packageNames.contains(packageName);
        }

        private static boolean isInstallPackage(String packageName) {
            return new File("/data/data/" + packageName).exists();
        }

        /**
         * 获取打开百度地图应用uri [http://lbsyun.baidu.com/index.php?title=uri/api/android]
         *
         * @param originLat
         * @param originLon
         * @param desLat
         * @param desLon
         * @return
         */
        public static String getBaiduMapUri(String originLat, String originLon, String originName, String desLat, String desLon, String destination, String region, String src) {
            String uri = "intent://map/direction?origin=latlng:%1$s,%2$s|name:%3$s" +
                    "&destination=latlng:%4$s,%5$s|name:%6$s&mode=driving&region=%7$s&src=%8$s#Intent;" +
                    "scheme=bdapp;package=com.baidu.BaiduMap;end";

            return String.format(uri, originLat, originLon, originName, desLat, desLon, destination, region, src);
        }




    /**
         * 网页版百度地图 有经纬度
         *
         * @param originLat
         * @param originLon
         * @param originName  ->注：必填
         * @param desLat
         * @param desLon
         * @param destination
         * @param region      : 当给定region时，认为起点和终点都在同一城市，除非单独给定起点或终点的城市。-->注：必填，不填不会显示导航路线
         * @param appName
         * @return
         */
        public static String getWebBaiduMapUri(String originLat, String originLon, String originName, String desLat, String desLon, String destination, String region, String appName) {
            String uri = "http://api.map.baidu.com/direction?origin=latlng:%1$s,%2$s|name:%3$s" +
                    "&destination=latlng:%4$s,%5$s|name:%6$s&mode=driving&region=%7$s&output=html" +
                    "&src=%8$s";
            return String.format(uri, originLat, originLon, originName, desLat, desLon, destination, region, appName);
        }


        /**
         * 打开本地百度地图
         * @param originLat
         * @param originLon
         * @param originName
         * @param desLat
         * @param desLon
         * @param desName
         * @return
         */
        public static String getAppBaiduMapUri(String originLon, String originLat, String originName,
                                               String desLon, String desLat, String desName){
            String uri = "baidumap://map/direction?origin=latlng:%1$s,%2$s|name:%3$s&" +
                    "destination=latlng:%4$s,%5$s|name:%6$s&mode=driving";

            return String.format(uri,originLat, originLon, originName, desLat, desLon, desName);
        }

        /**
         * 获取打开高德地图应用uri
         */
        public static String getAppGdMapUri(String appName, String slat, String slon, String sname, String dlat, String dlon, String dname) {
            String uri =  "androidamap://route?sourceApplication=%1$s"
                    + "&slat=%2$s&slon=%3$s&sname=%4$s&dlat=%5$s"
                    + "&dlon=%6$s&dname=%7$s&dev=0&t=0&showType=1";


            double[] sPoint = bdToGaoDe(Double.valueOf(slat),Double.valueOf(slon));
            double[] dPoint = bdToGaoDe(Double.valueOf(dlat),Double.valueOf(dlon));

            return String.format(uri, appName,sPoint[1] ,sPoint[0] , sname,dPoint[1] , dPoint[0], dname);
        }


    /**
         * 百度地图定位经纬度转高德经纬度
         *
         * @param bd_lat
         * @param bd_lon
         * @return
         */
        public static double[] bdToGaoDe(double bd_lat, double bd_lon) {
            double[] gd_lat_lon = new double[2];
            double PI = 3.14159265358979324 * 3000.0 / 180.0;
            double x = bd_lon - 0.0065, y = bd_lat - 0.006;
            double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
            double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
            gd_lat_lon[0] = z * Math.cos(theta);
            gd_lat_lon[1] = z * Math.sin(theta);
            return gd_lat_lon;
        }

        /**
         * 高德地图定位经纬度转百度经纬度
         *
         * @param gd_lon
         * @param gd_lat
         * @return
         */
        public static double[] gaoDeToBaidu(double gd_lon, double gd_lat) {
            double[] bd_lat_lon = new double[2];
            double PI = 3.14159265358979324 * 3000.0 / 180.0;
            double x = gd_lon, y = gd_lat;
            double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
            double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
            bd_lat_lon[0] = z * Math.cos(theta) + 0.0065;
            bd_lat_lon[1] = z * Math.sin(theta) + 0.006;
            return bd_lat_lon;
        }
}